package com.zidian.engine.service.impl;

import com.rmb.engine.dao.DictionaryDao;
import com.rmb.engine.entity.Dictionary;
import com.rmb.engine.service.DictionaryService;
import com.rmb.engine.dao.DictionaryDao;
import com.rmb.engine.entity.Dictionary;
import com.rmb.engine.service.DictionaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.test.annotation.Rollback;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

/**
 * @author ZhaoMyYang
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class DictionaryServiceImpl implements DictionaryService {

    @Autowired
    private DictionaryDao dictionaryDao;

    @Override
    public List<Dictionary> findList() {
        return dictionaryDao.findAll();
    }

    @Override
    public Dictionary addDictionary(Dictionary dictionary) {
        if (dictionary.getStatus()!=0){
            return dictionaryDao.save(dictionary);
        }else {
            return new Dictionary();
        }
    }

    @Override
    public Dictionary update(Dictionary dictionary) {

        if (dictionary.getStatus()!=0){
            return dictionaryDao.saveAndFlush(dictionary);
        }else {
            return new Dictionary();
        }
    }

    @Override
    public void del(Long id) {
        Optional<Dictionary> byId = dictionaryDao.findById(id);
        Dictionary dictionary = byId.get();
        dictionary.setStatus(0L);
        if (dictionary.getStatus()==0){
            dictionaryDao.saveAndFlush(dictionary);
        }
    }

}
